package com.sqly.doc.user;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cazaea.sweetalert.SweetAlertDialog;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.sqly.doc.R;
import com.sqly.doc.adapter.CommonAdapter;
import com.sqly.doc.adapter.ViewHolder;
import com.sqly.doc.base.MyBaseActivity;
import com.sqly.doc.menu.fjh.fjhdycrhsf;
import com.sqly.doc.pojo.Doctor;
import com.sqly.doc.pojo.RetMsg;
import com.sqly.doc.pojo.TeamMember;
import com.sqly.doc.util.HttpUrl;
import com.sqly.doc.util.MyToastUtil;
import com.sqly.doc.util.SharedPreferencesUtils;
import com.sqly.doc.util.SweetAlertDialogUtil;
import com.sqly.doc.util.UserClient;
import com.sqly.doc.view.StringUtils;
import com.sqly.doc.view.XListView;
import com.zyao89.view.zloading.ZLoadingDialog;
import com.zyao89.view.zloading.Z_TYPE;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

//import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * 查看团队人员信息
 * Created by Administrator on 2017/11/18 0018.
 */

public class team_member extends MyBaseActivity {
    List<TeamMember> list;//信息列表
    XListView listView;
    LinearLayout dd;
    ZLoadingDialog dialog;//加载动画
    Context con = team_member.this;
    private int pageNum1 = 1;// 无关键字页数
    //状态栏
    TextView title, uname;
    LinearLayout back;
    EditText tname;
    Button tj;
    Doctor j;

    @Override
    protected void initUI() {
        setContentView(R.layout.team_member);
        dd=(LinearLayout)findViewById(R.id.dd);
        listView = (XListView) findViewById(R.id.listView);
        listView.setPullLoadEnable(false);
        dialog = new ZLoadingDialog(con);
        dialog.setLoadingBuilder(Z_TYPE.DOUBLE_CIRCLE)//设置类型
                .setLoadingColor(Color.parseColor("#1C86EE"))//颜色
                .setHintText("Loading...")
                .setHintTextSize(16) // 设置字体大小 dp
                .setHintTextColor(Color.parseColor("#1C86EE"));  // 设置字体颜色
        //状态栏
        title = (TextView) findViewById(R.id.title);
        back = (LinearLayout) findViewById(R.id.back);
        title.setText("团队成员");
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        uname=(TextView)findViewById(R.id.uname);
        uname.setText("");
//        uname.setText("新增");
//        uname.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                startActivityForResult(new Intent(con, choose_doc.class).putExtra("cname", "team_member.class"), 1);
//            }
//        });
        tname = (EditText) findViewById(R.id.tname);
        tname.setText(getIntent().getStringExtra("names"));
        tj = (Button) findViewById(R.id.tj);
        tj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RequestParams ps = new RequestParams();
                ps.add("entity.id", getIntent().getStringExtra("teamid"));
                ps.add("entity.name", tname.getText().toString());
                ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
                ps.add("type", "2");
                UserClient.get(HttpUrl.EditTeam, ps, new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                        String content = new String(response);
                        RetMsg ret = JSON.parseObject(content, RetMsg.class);
                        if (ret.getCode().equals("200")) {
                            MyToastUtil.ShowToast(con, "成功");
                        }else{
                            MyToastUtil.ShowToast(con, ret.getInfo());
                        }
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

                    }
                });
            }
        });
    }

    @Override
    protected void initData() {
        if(SharedPreferencesUtils.getParam(con,"isxzgly","").equals("true")){
            uname.setVisibility(View.VISIBLE);
        }else{
            uname.setVisibility(View.GONE);
            tname.setVisibility(View.GONE);
            tj.setVisibility(View.GONE);
            dd.setVisibility(View.GONE);
        }
        getJumin();
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getJumin();
    }

    public void getJumin() {
        RequestParams ps = new RequestParams();
        ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
        ps.add("type", "2");
        ps.add("page", pageNum1 + "");
        ps.add("teamid", getIntent().getStringExtra("teamid"));
        UserClient.get(HttpUrl.GetTeamMember, ps, new AsyncHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
                dialog.show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {

                dialog.dismiss();
                MyToastUtil.ShowToast(con, "请求失败");
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                String content = new String(response);
                dialog.dismiss();
                RetMsg ret = JSON.parseObject(content, RetMsg.class);
                if (ret.getCode().equals("200")) {
                    if (pageNum1 == 1)
                        list = new ArrayList<TeamMember>();
                    List<TeamMember> ll = JSON.parseArray(
                            ret.getRd(), TeamMember.class);
                    JSONArray js = JSON.parseArray(ret.getRd());
                    for (int i = 0; i < js.size(); i++) {
                        JSONObject j = js.getJSONObject(i);
                        ll.get(i).setTmid(j.getString("id"));
                    }
                    for (int i = 0; i < ll.size(); i++) {
                        list.add(ll.get(i));
                    }

                    listView.setSelection(list.size() - ll.size());
                    listView.stopRefresh();
                    listView.stopLoadMore();
                    listView.setRefreshTime("刚刚");
                    listView.setXListViewListener(new XListView.IXListViewListener() {
                        @Override
                        public void onRefresh() {
                            pageNum1 = 1;
                            getJumin();
                        }

                        @Override
                        public void onLoadMore() {
                            pageNum1 = pageNum1 + 1;
                            getJumin();
                        }
                    });
                    listView.setAdapter(new CommonAdapter<TeamMember>(con, list, R.layout.team_member_item) {
                        @Override
                        public void convert(ViewHolder helper, TeamMember item) {
                            helper.setText(R.id.name, item.getMember_name());
                            helper.setText(R.id.member_code, item.getMember_code());
                            String lx = item.getMember_lx();
                            String lxStr = "";
                            if(!StringUtils.isEmpty(lx)){
                                if("1".equals(lx)){
                                    lxStr = "团队长";
                                }else if("2".equals(lx)){
                                    lxStr = "家庭医生";
                                }else if("3".equals(lx)){
                                    lxStr = "公卫人员";
                                }else if("4".equals(lx)){
                                    lxStr = "护士";
                                }else if("5".equals(lx)){
                                    lxStr = "信息人员";
                                }else if("6".equals(lx)){
                                    lxStr = "乡村医生";
                                }else if("7".equals(lx)){
                                    lxStr = "其他";
                                }
                            }
                            helper.setText(R.id.lx, lxStr);
                        }
                    });
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                            TeamMember teamMember = (TeamMember)list.get(position - 1);
                            if(null != teamMember && null != teamMember.getMember_lx() && !"".equals(teamMember.getMember_lx()) && "1".equals(teamMember.getMember_lx())){
                                if(teamMember.getMember_code().equals(SharedPreferencesUtils.getParam(con, "uid", "").toString())){
                                    Bundle b = new Bundle();
                                    b.putSerializable("member", teamMember);
                                    startActivity(new Intent(con, team_member_qm.class).putExtras(b));
                                }else{
                                    SweetAlertDialogUtil.WarningDialog(con, "提示", "禁止非团队长本人上传签名");
                                }
                            }
//                            if(SharedPreferencesUtils.getParam(con,"isxzgly","").equals("")){
//                            }else {
//                                new SweetAlertDialog(con, SweetAlertDialog.ERROR_TYPE)
//                                        .setTitleText("提示")
//                                        .setContentText("确定删除该成员么?")
//                                        .setConfirmText("确定")
//                                        .setCancelText("取消")
//                                        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
//                                            @Override
//                                            public void onClick(SweetAlertDialog sweetAlertDialog) {
//                                                sweetAlertDialog.dismiss();
//                                                RequestParams ps = new RequestParams();
//                                                ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
//                                                ps.add("type", "2");
//                                                ps.add("id", list.get(position - 1).getTmid());
//                                                UserClient.get(HttpUrl.DelTeamMember, ps, new AsyncHttpResponseHandler() {
//                                                    @Override
//                                                    public void onSuccess(int statusCode, Header[] headers, byte[] response) {
//                                                        String content = new String(response);
//                                                        RetMsg ret = JSON.parseObject(content, RetMsg.class);
//                                                        if (ret.getCode().equals("200")) {
//                                                            MyToastUtil.ShowToast(con, "成功");
//                                                        }else{
//                                                            MyToastUtil.ShowToast(con, ret.getInfo());
//                                                        }
//                                                        getJumin();
//                                                    }
//                                                });
//                                            }
//                                        })
//                                        .show();
//                            }

                        }
                    });

                } else {
                    MyToastUtil.ShowToast(con, "获取错误");
                }

            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            j = (Doctor) data.getExtras().getSerializable("j");
            new SweetAlertDialog(con, SweetAlertDialog.SUCCESS_TYPE)
                    .setTitleText("成员类型")
                    .setContentText("是否将该成员设置成负责人")
                    .setConfirmText("是")
                    .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                            sweetAlertDialog.dismiss();
                            RequestParams ps = new RequestParams();
                            ps.add("entity.team_id", getIntent().getStringExtra("teamid"));
                            ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
                            ps.add("type", "2");
                            ps.add("entity.member_code", j.getCode());
                            ps.add("entity.member_name", j.getName());
                            ps.add("entity.flag", "1");
                            UserClient.get(HttpUrl.AddTeamMember, ps, new AsyncHttpResponseHandler() {
                                @Override
                                public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                                    String content = new String(response);
                                    getJumin();
                                }

                                @Override
                                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

                                }
                            });
                        }
                    })
                    .setCancelText("否")
                    .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                            sweetAlertDialog.dismiss();
                            RequestParams ps = new RequestParams();
                            ps.add("entity.team_id", getIntent().getStringExtra("teamid"));
                            ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
                            ps.add("type", "2");
                            ps.add("entity.member_code", j.getCode());
                            ps.add("entity.member_name", j.getName());
                            ps.add("entity.flag", "0");
                            UserClient.get(HttpUrl.AddTeamMember, ps, new AsyncHttpResponseHandler() {
                                @Override
                                public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                                    String content = new String(response);
                                    getJumin();
                                }

                                @Override
                                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

                                }
                            });
                        }
                    })
                    .show();

        }
    }
}
